# coding=utf-8
import os
import logging
from logging import handlers

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))


class Config(object):
    """ 默认配置 """
    __mysql = dict(
        host='',
        user='',
        password='',
        db='',
        port=3306,
        charset='utf8')
    MYSQL_URL = "mysql://{user}:{password}@{host}:{port}/{db}?charset={charset}".format(**__mysql)


def configure_logging(is_stream_handler=False):
    """ Configures logging """

    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    logs_folder = os.path.join(CURRENT_DIR, "logs")
    if not os.path.exists(logs_folder):
        os.mkdir(logs_folder)
    formatter = logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')

    info_handler = handlers.RotatingFileHandler(
        os.path.join(logs_folder, 'info_log.out'),
        maxBytes=100000,
        backupCount=10
    )
    info_handler.setLevel(logging.INFO)
    info_handler.setFormatter(formatter)
    logger.addHandler(info_handler)

    error_handler = handlers.RotatingFileHandler(
        os.path.join(logs_folder, 'error_log.out'),
        maxBytes=100000,
        backupCount=10
    )
    error_handler.setLevel(logging.ERROR)
    error_handler.setFormatter(formatter)
    logger.addHandler(error_handler)

    if is_stream_handler:
        debug_handler = logging.StreamHandler()
        debug_handler.setLevel(logging.INFO)
        debug_handler.setFormatter(formatter)
        logger.addHandler(debug_handler)


configure_logging(is_stream_handler=True)
